package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.service.MemberService;
import com.itheima.service.OrderSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@DubboService(timeout = 10000)
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {




    @Override
    public void importData(List<String[]> list) {
        if(list != null){
            List<OrderSetting> orderSettings = new ArrayList<>();
            for(String[] rowData : list){
                Date orderDate = DateUtil.parseDate(rowData[0]);
                Integer number = Integer.parseInt(rowData[1]);
                //1.根据日期查询已有数据
                OrderSetting orderSetting = this.findByDate(rowData[0]);
                if(orderSetting == null){//该日期不存在数据，需要插入数据库中
                    orderSetting = new OrderSetting();
                }
                //2.维护数据
                orderSetting.setOrderDate(orderDate);
                orderSetting.setNumber(number);
                orderSettings.add(orderSetting);
            }
            //3.批量修改或新增
            this.saveOrUpdateBatch(orderSettings);
        }
    }

    @Override
    public OrderSetting findByDate(String orderDate) {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_date",orderDate);
        return this.getOne(queryWrapper);
    }

    @Override
    public Map<String,OrderSetting> findByYearMonth(Integer year,Integer month) {
        String yearMonth = year + "-" + ((month < 10) ? "0" : "") + month;
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("order_date",yearMonth);
        //根据年月查询数据库数据
        List<OrderSetting> list = this.list(queryWrapper);
        Map<String,OrderSetting> map = new HashMap<>();
        //组装数据
        for(OrderSetting orderSetting : list){
            String orderDate = DateUtil.formatDate(orderSetting.getOrderDate());
            map.put(orderDate,orderSetting);
        }
        return map;
    }

    @Override
    public boolean oneDaySetting(String day, Integer num) {
        OrderSetting orderSetting = this.findByDate(day);
        if(orderSetting == null){//该日期不存在数据，需要插入数据库中
            orderSetting = new OrderSetting();
        }
        //2.维护数据
        orderSetting.setOrderDate(DateUtil.parseDate(day));
        orderSetting.setNumber(num);
        return this.saveOrUpdate(orderSetting);
    }

    @Override
    public int isOrderOkByOrderDate(String orderDate) {
        //  根据日期 查询有无预约满
        return baseMapper.isOrderOkByOrderDate(orderDate);
    }

    @Override
    public void updateReservationsByOrderDate(String orderDate) {
        baseMapper.updateReservationsByOrderDate(orderDate);
    }

}
